package com.lhy.admin.common.mvcplus.entity;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;

/**
 * RespResult 返回的实体对象
 *
 * @author hyluan
 * @date 2022/6/24 17:50
 */
@Schema(title = "ResultResp（响应结果）", description = "数据")
@Data
public class RespResult<T> implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public static final int SUCCESS = 0;
    public static final int ERROR = -1;

    /**
     *
     */
    @Schema(title = "code", description = "返回状态码 0代表成功", required = true)
    private int code;

    @Schema(description = "返回消息")
    private String msg;

    @Schema(description = "返回的业务数据")
    private T data;

    public RespResult() {
    }

    public static <T> RespResult<T> ok() {
        return new RespResult<T>().setCode(SUCCESS);
    }

    public static <T> RespResult<T> ok(T data) {
        RespResult<T> ok = ok();
        ok.setMsg("成功");
        return ok.setData(data);
    }

    public static <T> RespResult<T> fail() {
        return new RespResult<T>().setCode(ERROR);
    }

    public static <T> RespResult<T> fail(String msg) {
        return new RespResult<T>().setCode(ERROR).setMsg(msg);
    }

    public static <T> RespResult<T> create(int record) {
        return new RespResult<>(record);
    }

    public static <T> RespResult<T> create(boolean ret) {
        return ret ? ok() : fail();
    }

    RespResult(int record) {
        super();
        this.code = record > 0 ? SUCCESS : ERROR;
    }

}
